/*
 * Copyright (C) 2014 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package traderlib.core.util;

import java.math.BigDecimal;

/**
 * Number utilities extended from Apache Commons Lang.
 *
 * @author Miquel Sas
 */
public class NumberUtils extends org.apache.commons.lang3.math.NumberUtils {

	/**
	 * Returns the average of a list of values.
	 *
	 * @param ds The list of double values.
	 * @return The average
	 */
	public static double average(double... ds) {
		double avg = 0;
		if (ds != null) {
			for (int i = 0; i < ds.length; i++) {
				avg += ds[i];
			}
			avg /= ds.length;
		}
		return avg;
	}

	/**
	 * Round a number.
	 * <p>
	 * @param value The value to round.
	 * @param decimals The number of decimal places.
	 * @return The rounded value.
	 */
	public static double round(double value, int decimals) {
		double p = java.lang.Math.pow(10, decimals);
		double v = value * p;
		long l = java.lang.Math.round(v);
		double r = l / p;
		return r;
	}

	public static String defaultStringNumber(int decimals) {
		StringBuilder b = new StringBuilder();
		b.append("0");
		if (decimals > 0) {
			b.append(".");
			b.append(StringUtils.repeat("0", decimals));
		}
		return b.toString();
	}

	/**
	 * Returns a big decimal.
	 *
	 * @return The <code>BigDecimal</code>.
	 * @param str The text string.
	 * @param dec Tne decimal places.
	 * @param decPoint A flag telling if the decimal point is included.
	 */
	public static BigDecimal getBigDecimal(String str, int dec, boolean decPoint) {
		if (dec == 0) {
			return new BigDecimal(str);
		}
		int len = str.length();
		if (decPoint) {
			return new BigDecimal(str).setScale(dec, BigDecimal.ROUND_HALF_UP);
		} else {
			return new BigDecimal(str.substring(0, len - dec) + "." + str.substring(len - dec)).setScale(dec, BigDecimal.ROUND_HALF_UP);
		}
	}

	/**
	 * Verifies and corrects numeric format.
	 *
	 * @return The result string.
	 * @param str The source string.
	 */
	public static String numberFormat(String str) {
		// Strip spaces at both ends.
		str = str.trim();
		// Sign can be on the left and on the right. if its on the left, there can be
		// spaces between the sign and the number itself.
		int len = str.length();
		boolean sign
			= str.charAt(0) == '-' || str.charAt(0) == '+'
			|| str.charAt(len - 1) == '-' || str.charAt(len - 1) == '+';
		// If there is a sign, put it in the right place.
		if (sign) {
			if (str.charAt(0) == '-' || str.charAt(0) == '+') {
				if (str.charAt(1) == ' ') {
					str = str.substring(0, 1) + str.substring(1).trim();
				}
			} else {
				str = str.substring(len - 1, len) + str.substring(0, len - 1);
			}
			// Strip positive sign.
			if (str.charAt(0) == '+') {
				str = str.substring(1);
			}
		}
		return str;
	}
}
